package top.gbiot.ai.controller;



import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

import static java.awt.SystemColor.window;

@Controller
@RequestMapping("/iot/ai")
public class IotAiController {

    protected boolean sucess=false;
    String prefix ="iot/ai";


    @RequiresPermissions("iot:ai:view")
    @GetMapping()
    //@ResponseBody
    public String  ai() throws IOException {

        int status= -1;
        String message="";
        String body="";
        String input = "{\"username\":\"hxgk\", \"password\":\"hxgk1234\"}";

        HttpURLConnection conn = (HttpURLConnection) new URL("http://192.168.100.200/api/system/user/login").openConnection();
        try {
            conn.setConnectTimeout(300);
            conn.setRequestMethod("POST");
            conn.setRequestProperty("Content-Type", "application/json; utf-8");
            conn.setRequestProperty("Accept", "application/json");
            conn.setDoOutput(true);

            OutputStream os = conn.getOutputStream();
            byte[] inputBytes = input.getBytes("utf-8");
            os.write(inputBytes);
            os.flush();

            status = conn.getResponseCode();
            message = conn.getResponseMessage();


            //读取返回信息
            String texts;
            BufferedReader ins = new BufferedReader(new InputStreamReader(conn.getInputStream()));
            StringBuilder content = new StringBuilder();
            while( (texts = ins.readLine()) != null) {
                content.append(texts);
            }
            ins.close();
            body = content.toString();


        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if ( null != conn) {
                conn.disconnect();
            }
        }

        System.out.println("Status code: " + status);
        System.out.println("Return massage: " +  message);
        System.out.println("body content: " + body);

        return prefix +"/entry";
    }


    public void  openInWeb(){

    }
}



